package com.jeecms.cms.action.front;

import com.jeecms.cms.entity.main.Content;
import com.jeecms.cms.entity.main.ContentAttachment;
import com.jeecms.cms.manager.main.ContentCountMng;
import com.jeecms.cms.manager.main.ContentMng;
import com.jeecms.common.security.encoder.PwdEncoder;
import com.jeecms.common.web.ResponseUtils;
import com.jeecms.common.web.springmvc.RealPathResolver;
import com.jeecms.core.entity.CmsConfig;
import com.jeecms.core.entity.CmsSite;
import com.jeecms.core.entity.CmsUser;
import com.jeecms.core.web.WebErrors;
import com.jeecms.core.web.util.CmsUtils;
import com.jeecms.core.web.util.FrontUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
public class AttachmentAct {
	private static final Logger log = LoggerFactory.getLogger(AttachmentAct.class);

	@RequestMapping(value = "/attachment.jspx", method = RequestMethod.GET)
	public void attachment(Integer cid, Integer i, Long t, String k, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws IOException {
		if (cid == null) {
			ResponseUtils.renderText(response, "downlaod error!");
		}
		CmsConfig config = CmsUtils.getSite(request).getConfig();
		String code = config.getDownloadCode();
		int h = config.getDownloadTime() * 60 * 60 * 1000;
		if (pwdEncoder.isPasswordValid(k, cid + ";" + i + ";" + t, code)) {
			long curr = System.currentTimeMillis();
			if (t + h > curr) {
				Content c = contentMng.findById(cid);
				if (c != null) {
					List<ContentAttachment> list = c.getAttachments();
					if (list.size() > i) {
						contentCountMng.downloadCount(c.getId());
						ContentAttachment ca = list.get(i);
						response.sendRedirect(ca.getPath());
						return;
					} else {
						log.info("download index is out of range: {}", i);
					}
				} else {
					log.info("Content not found: {}", cid);
				}
			} else {
				log.info("download time is expired!");
			}
		} else {
			log.info("download key error!");
		}
		ResponseUtils.renderText(response, "downlaod error!");
	}

	/**
	 * 获得下载key和time
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/attachment_url.jspx", method = RequestMethod.GET)
	public void url(Integer cid, Integer n, HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		if (cid == null || n == null) {
			return;
		}
		CmsConfig config = CmsUtils.getSite(request).getConfig();
		String code = config.getDownloadCode();
		long t = System.currentTimeMillis();
		JSONArray arr = new JSONArray();
		String key;
		for (int i = 0; i < n; i++) {
			key = pwdEncoder.encodePassword(cid + ";" + i + ";" + t, code);
			arr.put("&t=" + t + "&k=" + key);
		}
		ResponseUtils.renderText(response, arr.toString());
	}

    /**
     * 文库文档下载
     * @param contentId 文库内容ID
     * @param request 请求
     * @param response 回应
     * @param model 参数存放
     * @return 文库文档下载
     * @throws IOException 文件不存在
     */
    @RequestMapping(value = "/downdoc.jspx", method = RequestMethod.GET)
	public String down(Integer contentId, HttpServletRequest request, HttpServletResponse response, ModelMap model)
			throws IOException {
		CmsSite site = CmsUtils.getSite(request);
		CmsUser user = CmsUtils.getUser(request);
		FrontUtils.frontData(request, model, site);
		WebErrors errors = WebErrors.create(request);
		if (contentId == null) {
			errors.addErrorCode("error.required", "contentId");
			return FrontUtils.showError(request, response, model, errors);
		}
		if (user == null) {
			return FrontUtils.showLogin(request, model, site);
		}
        Content content = contentMng.findById(contentId);
        if (content == null) {
            return FrontUtils.showMessage(request, model,
                    "comment.contentNotFound");
        }

		if (content.getAttachments() == null || content.getAttachments().size() <= 0) {
			errors.addErrorCode("error.hasNotFundDoc");
			return FrontUtils.showError(request, response, model, errors);
		} else {
			boolean hasNeedGrain = checkUserGrain(user, content.getDownNeed());
			if (hasNeedGrain) {
				CmsUser downUser = CmsUtils.getUser(request);
//				contentDocMng.operateDocGrain(downUser, doc);
				String errorMsg = downLoadFile(request, response, content);
				if(content.getDownNeed() != null && content.getDownNeed() > 0){
				    CmsUser docOwner = content.getUser();
				    //下载用户财富值减少
                    downUser.setGrain(downUser.getGrain() - content.getDownNeed());
                    //文档上传者财富值增加
                    docOwner.setGrain(docOwner.getGrain() + content.getDownNeed());
                }
                //增加下载次数
                contentCountMng.downloadCount(contentId);

				if(StringUtils.isNotBlank(errorMsg)){
                    errors.addErrorCode(errorMsg);
				    return FrontUtils.showError(request, response, model, errors);
                }
			} else {
				errors.addErrorCode("error.hasNoEnoughGrain", content.getDownNeed());
				return FrontUtils.showError(request, response, model, errors);
			}
			return null;
		}
	}

    private boolean checkUserGrain(CmsUser user, Integer downNeed) {
        if (user.getGrain() >= downNeed) {
            return true;
        } else {
            return false;
        }
    }

    private String downLoadFile(HttpServletRequest request, HttpServletResponse response, Content content)
            throws IOException {
        List<ContentAttachment> docList = content.getAttachments();
        if(docList != null && docList.size()>0){
            ContentAttachment ca = docList.get(0);
            String attachPath = ca.getPath(); //存储文件路径+名称  原格式的
            String fileName = ca.getFilename(); //原版格式的文件名

            // 文件绝对路径
            String fileRealPath = realPathResolver.get(attachPath);
            File file = new File(fileRealPath);
            if (file.exists()) {
                byte[] byteArray = FileUtils.readFileToByteArray(file);
                try {
                    fileName = new String(fileName.getBytes(), "ISO8859-1");
                } catch (UnsupportedEncodingException e) {
                    // e.printStackTrace();
                }
                long fileLength = new File(fileRealPath).length();
                response.setHeader("Content-Length", String.valueOf(fileLength));
                response.setContentType("application/octet-stream;charset=ISO8859-1");
                response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
                ServletOutputStream out = response.getOutputStream();
                out.write(byteArray);
                out.close();
                return null;
            }
        }
        return "content.doc.hasDelete";
    }

	@Autowired
	private ContentMng contentMng;
	@Autowired
	private ContentCountMng contentCountMng;
	@Autowired
	private PwdEncoder pwdEncoder;
	@Autowired
	private RealPathResolver realPathResolver;
}
